<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="python,打包,pyinstaller,">










<meta name="description" content="PyInstaller简述pyinstaller自身打包的流程：读取编写好的python脚本，分析其中调用的模块和库，然后收集这些文件的副本(包括Python的解释器)。最后把副本与脚本,可执行文件等放在一个文件夹中，或者可选的封装在一个可执行文件中。 pyinstaller在Windows平台上的使用环境：  Python     2.7 或 3.4-3.7    Windows(32位/64位">
<meta name="keywords" content="python,打包,pyinstaller">
<meta property="og:type" content="article">
<meta property="og:title" content="PyInstaller打包详解">
<meta property="og:url" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/index.html">
<meta property="og:site_name" content="Yu&#39;s Home">
<meta property="og:description" content="PyInstaller简述pyinstaller自身打包的流程：读取编写好的python脚本，分析其中调用的模块和库，然后收集这些文件的副本(包括Python的解释器)。最后把副本与脚本,可执行文件等放在一个文件夹中，或者可选的封装在一个可执行文件中。 pyinstaller在Windows平台上的使用环境：  Python     2.7 或 3.4-3.7    Windows(32位/64位">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539867547078.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539867804758.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539868505951.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539868726067.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539868755008.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539869310726.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539869506562.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539869571287.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539870806724.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539871065751.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539871302725.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539871753093.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539872210943.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539872746864.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539872763233.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539872867114.png">
<meta property="og:updated_time" content="2023-02-23T12:31:14.839Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PyInstaller打包详解">
<meta name="twitter:description" content="PyInstaller简述pyinstaller自身打包的流程：读取编写好的python脚本，分析其中调用的模块和库，然后收集这些文件的副本(包括Python的解释器)。最后把副本与脚本,可执行文件等放在一个文件夹中，或者可选的封装在一个可执行文件中。 pyinstaller在Windows平台上的使用环境：  Python     2.7 或 3.4-3.7    Windows(32位/64位">
<meta name="twitter:image" content="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/1539867547078.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '9PFXHHRD2G',
      apiKey: '45293228d8a5cecc1c0826fb3d34a130',
      indexName: 'blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"没有找到任何结果: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/">





  <title>PyInstaller打包详解 | Yu's Home</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1746d8de4fb78edd21f9004d348fa6be";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Yu's Home</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">管它真理无穷</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于Yu
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Yu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xiaomai.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yu's Home">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">PyInstaller打包详解</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-18T17:39:22+08:00">
                2018-10-18
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
              <i class="fa fa-comment-o"></i>
              </span>
              <a href="/2018/10/18/PyInstaller打包详解/#comments" itemprop="discussionUrl">
              <span class="post-comments-count valine-comment-count" data-xid="/2018/10/18/PyInstaller打包详解/" itemprop="commentCount"></span>
              </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,080
                </span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="PyInstaller简述"><a href="#PyInstaller简述" class="headerlink" title="PyInstaller简述"></a>PyInstaller简述</h2><p><strong>pyinstaller自身打包的流程</strong>：读取编写好的python脚本，分析其中调用的模块和库，然后收集这些文件的副本(包括Python的解释器)。最后把副本与脚本,可执行文件等放在一个文件夹中，或者可选的封装在一个可执行文件中。</p>
<p><strong>pyinstaller在Windows平台上的使用环境</strong>：</p>
<ul>
<li>Python </li>
</ul>
<blockquote>
<ul>
<li>2.7 或 3.4-3.7</li>
</ul>
</blockquote>
<ul>
<li>Windows(32位/64位)</li>
</ul>
<blockquote>
<ul>
<li>Windows XP或更高版本</li>
</ul>
</blockquote>
<h4 id="PyInstaller相关参数："><a href="#PyInstaller相关参数：" class="headerlink" title="PyInstaller相关参数："></a>PyInstaller相关参数：</h4><a id="more"></a> 

<table>
<thead>
<tr>
<th>-F, –onefile</th>
<th>打包一个单个文件，如果你的代码都写在一个.py文件的话，可以用这个，如果是多个.py文件就别用</th>
</tr>
</thead>
<tbody><tr>
<td>-D, –onedir</td>
<td>打包多个文件，在dist中生成很多依赖文件，适合以框架形式编写工具代码，我个人比较推荐这样，代码易于维护</td>
</tr>
<tr>
<td>-K, –tk</td>
<td>在部署时包含 TCL/TK</td>
</tr>
<tr>
<td>-a, –ascii</td>
<td>不包含编码.在支持Unicode的python版本上默认包含所有的编码.</td>
</tr>
<tr>
<td>-d, –debug</td>
<td>产生debug版本的可执行文件</td>
</tr>
<tr>
<td><strong>-w,–windowed,–noconsole</strong></td>
<td>使用Windows子系统执行.当程序启动的时候不会打开命令行(只对Windows有效)</td>
</tr>
<tr>
<td><strong>-c,–nowindowed,–console</strong></td>
<td>使用控制台子系统执行(默认)(只对Windows有效)pyinstaller -c  xxxx.pypyinstaller xxxx.py –console</td>
</tr>
<tr>
<td>-s,–strip</td>
<td>可执行文件和共享库将run through strip.注意Cygwin的strip往往使普通的win32 Dll无法使用.</td>
</tr>
<tr>
<td>-X, –upx</td>
<td>如果有UPX安装(执行Configure.py时检测),会压缩执行文件(Windows系统中的DLL也会)(参见note)</td>
</tr>
<tr>
<td>-o DIR, –out=DIR</td>
<td>指定spec文件的生成目录,如果没有指定,而且当前目录是PyInstaller的根目录,会自动创建一个用于输出(spec和生成的可执行文件)的目录.如果没有指定,而当前目录不是PyInstaller的根目录,则会输出到当前的目录下.</td>
</tr>
<tr>
<td>-p DIR, –path=DIR</td>
<td>设置导入路径(和使用PYTHONPATH效果相似).可以用路径分割符(Windows使用分号,Linux使用冒号)分割,指定多个目录.也可以使用多个-p参数来设置多个导入路径，让pyinstaller自己去找程序需要的资源</td>
</tr>
<tr>
<td>–icon=&lt;FILE.ICO&gt;</td>
<td>将file.ico添加为可执行文件的资源(只对Windows系统有效)，改变程序的图标  pyinstaller -i  ico路径 xxxxx.py</td>
</tr>
<tr>
<td>–icon=&lt;FILE.EXE,N&gt;</td>
<td>将file.exe的第n个图标添加为可执行文件的资源(只对Windows系统有效)</td>
</tr>
<tr>
<td>-v FILE, –version=FILE</td>
<td>将verfile作为可执行文件的版本资源(只对Windows系统有效)</td>
</tr>
<tr>
<td>-n NAME, –name=NAME</td>
<td>可选的项目(产生的spec的)名字.如果省略,第一个脚本的主文件名将作为spec的名字</td>
</tr>
</tbody></table>
<h2 id="一、流程"><a href="#一、流程" class="headerlink" title="一、流程"></a>一、流程</h2><p>pyinstaller作为python打包使用最多的库，其兼容性无疑是极佳的。但既然是外部包，肯定会存在与python版本甚至外部包的兼容问题和较严格的使用规范。</p>
<p>这里以一个QT+matplotlib实现的图形界面为例来讲解pyinstaller其中的坑</p>
<p>文件结构目录：（main.py为主入口程序）<img src="/2018/10/18/PyInstaller打包详解/1539867547078.png" alt="1539867547078"></p>
<blockquote>
<p><strong>第一步：安装pyinstaller</strong></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install pyinstaller</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>第二步：打开命令行，进入要打包的主程序目录下</strong></p>
</blockquote>
<p><strong>输入pyi-makespec -w main.py并回车</strong>（多文件GUI应用使用-w参数，建立spec文件，后面会讲解）</p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539867804758.png" alt="1539867804758"></p>
<p><strong>打开生成在当前目录下的main.spec文件</strong></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539868505951.png" alt="1539868505951"></p>
<p><strong>添加项目所需的属性</strong>（<strong>后面会说明添加的规范</strong>）</p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539868726067.png" alt="1539868726067"></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539868755008.png" alt="1539868755008"></p>
<blockquote>
<p><strong>第三步：执行打包命令</strong></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pyinstaller main.spec</span><br><span class="line">--这里加不加参数都可以，pyinstaller的参数本质就是spec里的配置属性</span><br></pre></td></tr></table></figure>

<p><img src="/2018/10/18/PyInstaller打包详解/1539869310726.png" alt="1539869310726"></p>
<blockquote>
<p><strong>第四步：查看目录结构，执行测试</strong></p>
</blockquote>
<p><strong>打包完成后目录多了bulid,dist目录，dist目录下存放着最终可发布的打包目录</strong></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539869506562.png" alt="1539869506562"></p>
<p><strong>双击main.exe执行</strong></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539869571287.png" alt="1539869571287"></p>
<h2 id="二、规范"><a href="#二、规范" class="headerlink" title="二、规范"></a>二、规范</h2><h4 id="2-1-打包参数的使用"><a href="#2-1-打包参数的使用" class="headerlink" title="2.1 打包参数的使用"></a>2.1 打包参数的使用</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">--paths=C:\xxx		<span class="comment"># 用于指定pyinstaller搜索包的路径，通常不需要指定</span></span><br><span class="line">    				<span class="comment"># 因为site-packages下的都会被搜索</span></span><br><span class="line">--icon=C:\xxx <span class="comment"># 用于设置exe程序的ico图标，亲测必须为ico且要求ico图片分辨率(建议64x64)</span></span><br><span class="line">    </span><br><span class="line"><span class="comment"># 需要打包一个单独的.py文件时</span></span><br><span class="line">-F	<span class="comment"># 带控制台</span></span><br><span class="line">-F -w	<span class="comment"># 不带控制台，直接以Windows窗体打开</span></span><br><span class="line"><span class="comment"># 需要打包多py文件的项目时</span></span><br><span class="line">-w或-D -w	<span class="comment"># 不带控制台，直接以windows窗体打开</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 第一次打包时建议用-c模式，让程序以控制台子系统执行，方便调试，无错误后用-F或-w进行打包</span></span><br></pre></td></tr></table></figure>

<h4 id="2-2-Spec文件的使用"><a href="#2-2-Spec文件的使用" class="headerlink" title="2.2 Spec文件的使用"></a>2.2 Spec文件的使用</h4><p><strong>2.2.1 添加多文件打包</strong></p>
<p>在打包多文件的项目中，需把每个py文件的路径添加到Analysis的第一个字段里</p>
<p><strong>PS</strong>：<u><strong><em>和main.py同级目录下的py文件可以不用添加</em></strong></u></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539870806724.png" alt="1539870806724"></p>
<p><strong>2.2.2 添加外部dll文件及设置exe图标</strong></p>
<p>pyd,dll文件放入到binaries元组中，第一个参数路径可以使用通配符，第二个参数为打包后的目录相对路径</p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539871065751.png" alt="1539871065751"></p>
<p>把ico放入到main.py同级目录下，可直接用文件名，ico建议用64*64像素</p>
<p>图标资源可通过阿里巴巴矢量图库获取：<a href="http://www.iconfont.cn/" target="_blank" rel="noopener">http://www.iconfont.cn/</a></p>
<p>提供一个png,jpeg等图片格式转ico各种大小的网站：<a href="https://lvwenhan.com/convertico/" target="_blank" rel="noopener">https://lvwenhan.com/convertico/</a></p>
<p>PS：离开了图标所在的文件夹可能会变回默认图标，刷新或操作后恢复</p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539871302725.png" alt="1539871302725"></p>
<p><strong>2.2.3 图片等资源文件的添加与封装</strong></p>
<p>图片文件的添加有两种方式：</p>
<blockquote>
<p>第一种：当程序中的图片地址为相对路径时，直接把图片目录放入打包后的文件中或通过spec配置。但当图片被用户删除或路径改变，程序就获取不到资源了！（不推荐）</p>
</blockquote>
<p><img src="/2018/10/18/PyInstaller打包详解/1539871753093.png" alt="1539871753093"></p>
<blockquote>
<p>第二种：把图片等资源封装到exe中，但前提要求是程序中的资源路径都得改为绝对路径，因为打包为单exe文件中，分析运行文件时利用Tree函数形成一个文件目录树，运行时将这些文件 释放至C:UsersUSERNAMEAppDataLocal下的一个临时文件夹XXXXX内。程序自身引用此目录的绝对路径来获得所需的文件。</p>
</blockquote>
<p>第一步：先将程序中的相对路径更改为绝对路径</p>
<p>第二步：在Spec文件中添加参数，Tree函数将所有文件均添加为’DATA’型数据，其结构为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[(‘解压后地址’, ‘文件地址’, ‘类型参数’)]</span><br><span class="line"><span class="comment"># 在打包完成后把第一个参数对应资源路径的文件删除即可</span></span><br></pre></td></tr></table></figure>

<p><img src="/2018/10/18/PyInstaller打包详解/1539872210943.png" alt="1539872210943"></p>
<p><strong><em>总结：</em></strong> 1.所有的路径都必须使用 “\\”作为分隔符，且路径不能带有中文</p>
<p>2.spec中可进行打包参数的编辑，在之后可以不用再加参数直接pyinstaller</p>
<p>3.资源文件的打包建议使用2.2.3中第二种方式</p>
<h2 id="三、问题"><a href="#三、问题" class="headerlink" title="三、问题"></a>三、问题</h2><h4 id="3-1-RecursionError-maximum-recursion-depth-exceeded"><a href="#3-1-RecursionError-maximum-recursion-depth-exceeded" class="headerlink" title="3.1 RecursionError: maximum recursion depth exceeded"></a>3.1 RecursionError: maximum recursion depth exceeded</h4><p>这是出现在pyinstaller打包时的问题，中文意思是超出最大递归深度</p>
<p>解决方法：退至低版本的python可以得到解决，原因还是在兼容性</p>
<p><strong>PyInstaller 3.4 + Python 3.6.6 亲测失败</strong></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539872746864.png" alt="1539872746864"></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539872763233.png" alt="1539872763233"></p>
<p><strong>PyInstaller 3.3.1 + Python 3.6.5 亲测成功</strong></p>
<p><img src="/2018/10/18/PyInstaller打包详解/1539872867114.png" alt="1539872867114"></p>
<h4 id="3-2-双击程序Failed-to-excute-Script-main"><a href="#3-2-双击程序Failed-to-excute-Script-main" class="headerlink" title="3.2 双击程序Failed to excute Script main"></a>3.2 双击程序Failed to excute Script main</h4><p>本质上还是模块缺失了，建议使用-c模式重新打包调试，找到缺失的模块pip install即可解决</p>
<h4 id="3-3-文件打包后过大的问题"><a href="#3-3-文件打包后过大的问题" class="headerlink" title="3.3 文件打包后过大的问题"></a>3.3 文件打包后过大的问题</h4><p>这个问题本质上其实是没办法解决的，因为pyinstaller不是编译，而是将py程序与相应的库打包成可执行文件，大小和速度是和你调用的库的大小与速度成正比的，所以在写python程序的过程中尽量不要使用import，而是用from…import…来减少调用。如果是import的话，在打包的时候，会将整个包都打包到exe里面，没有意义的增大了工具的大小！</p>
<h4 id="3-4-防止反编译的问题"><a href="#3-4-防止反编译的问题" class="headerlink" title="3.4 防止反编译的问题"></a>3.4 防止反编译的问题</h4><p>按上述步骤打包的程序是容易被反编译为pyc然后破译出main入口的源码的，</p>
<p>为了安全起见，可以通过工具把py转为pyd来调用后打包</p>
<p>链接：<a href="http://www.lizenghai.com/archives/898.html" target="_blank" rel="noopener">http://www.lizenghai.com/archives/898.html</a></p>
<h2 id="四、更多"><a href="#四、更多" class="headerlink" title="四、更多"></a>四、更多</h2><p>github:<a href="https://github.com/pyinstaller/pyinstaller" target="_blank" rel="noopener">https://github.com/pyinstaller/pyinstaller</a></p>
<p>官方doc​:<a href="https://pythonhosted.org/PyInstaller/requirements.html" target="_blank" rel="noopener">https://pythonhosted.org/PyInstaller/requirements.html</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>觉得好的话就打赏Ta一瓶冰阔落吧</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Yu 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Yu 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    Yu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/" title="PyInstaller打包详解">https://yujunjiex.gitee.io/2018/10/18/PyInstaller打包详解/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/python/" rel="tag"># python</a>
          
            <a href="/tags/打包/" rel="tag"># 打包</a>
          
            <a href="/tags/pyinstaller/" rel="tag"># pyinstaller</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/09/26/SQL-Server-SSMS配置经验/" rel="next" title="SQL Server+SSMS配置经验">
                <i class="fa fa-chevron-left"></i> SQL Server+SSMS配置经验
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/31/谈谈计算机中的编码问题/" rel="prev" title="谈谈计算机中的编码问题">
                谈谈计算机中的编码问题 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_inline_share_toolbox">
  <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5b03d4a278f58ab9" async="async"></script>
</div>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/xiaomai.png" alt="Yu">
            
              <p class="site-author-name" itemprop="name">Yu</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">30</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yujunjiex" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#PyInstaller简述"><span class="nav-number">1.</span> <span class="nav-text">PyInstaller简述</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#PyInstaller相关参数："><span class="nav-number">1.0.1.</span> <span class="nav-text">PyInstaller相关参数：</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#一、流程"><span class="nav-number">2.</span> <span class="nav-text">一、流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二、规范"><span class="nav-number">3.</span> <span class="nav-text">二、规范</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-打包参数的使用"><span class="nav-number">3.0.1.</span> <span class="nav-text">2.1 打包参数的使用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-Spec文件的使用"><span class="nav-number">3.0.2.</span> <span class="nav-text">2.2 Spec文件的使用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三、问题"><span class="nav-number">4.</span> <span class="nav-text">三、问题</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-RecursionError-maximum-recursion-depth-exceeded"><span class="nav-number">4.0.1.</span> <span class="nav-text">3.1 RecursionError: maximum recursion depth exceeded</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-双击程序Failed-to-excute-Script-main"><span class="nav-number">4.0.2.</span> <span class="nav-text">3.2 双击程序Failed to excute Script main</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-文件打包后过大的问题"><span class="nav-number">4.0.3.</span> <span class="nav-text">3.3 文件打包后过大的问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-防止反编译的问题"><span class="nav-number">4.0.4.</span> <span class="nav-text">3.4 防止反编译的问题</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#四、更多"><span class="nav-number">5.</span> <span class="nav-text">四、更多</span></a></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Yu</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">46.7k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a></div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>

<script type="text/javascript" src="/custom/fold_action.js?v="></script>





  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//code.bdstatic.com/npm/leancloud-storage@4.12.0/dist/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'rg0EMRYKYKMhOh1zwqEKS5jz-gzGzoHsz',
        appKey: 'T04zQc0OzQd1uxOqcVomoGqj',
        placeholder: '(= =)~*只有10086嘘寒问暖',
        avatar:'mm',
        guest_info:guest,
        pageSize:'5' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  


  

  

  
  

  

  

  


</body>
</html>

